package com.ymh.spring.learn.profile.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
 * 
 * 名称：DataSourceProfileConfig<br/> 
 * 描述：@Profile注解基于激活的profile实现bean的装配<br/>
 * 创建时间：2018年3月20日 下午5:30:30 <br/>
 * 
 * @author aivin
 * @version 1.0.0
 */
@Configuration
public class DataSourceProfileConfig {

	
	/**
	 * 
	 * <h4>功能：使用一个嵌入式数据库</h4>
	 * 
	 * @return
	 */
	@Bean(destroyMethod = "shutdown")
	public DataSource embeddedDataSource() {
		return new EmbeddedDatabaseBuilder().addScript("classpath:schema.sql")
				.addScript("classpath:test-data.sql").build();
	}

	/**
	 *
	 * <h4>功能：使用JNDI数据源</h4>
	 * 
	 * @return
	 */
	@Bean
	public DataSource jndiDataSource() {
		JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
		jndiObjectFactoryBean.setJndiName("jdbs/MyDS");
		jndiObjectFactoryBean.setResourceRef(true);
		jndiObjectFactoryBean.setProxyInterface(DataSource.class);
		return (DataSource) jndiObjectFactoryBean.getObject();
	}

}
